﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace zwinForm
{
    public partial class MainFrame : BaseFrame
    {
        public MainFrame()
        {
            InitializeComponent();
        }

        private void 日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", System.Environment.CurrentDirectory + "\\Log");
        }

        private void 最小化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Minimize();
        }
        /// <summary>
        /// 最小化
        /// </summary>
        private void Minimize()
        {
            this.WindowState = FormWindowState.Minimized;
            notifyIcon1.Visible = true;
            this.Hide();
            notifyIcon1.BalloonTipText = this.Text + "正在后台运行";
            notifyIcon1.BalloonTipTitle = "提示";
            notifyIcon1.BalloonTipIcon = ToolTipIcon.None;
            notifyIcon1.ShowBalloonTip(5000);
            notifyIcon1.Text = this.Text;
        }

        /// <summary>
        /// 从最小化显示出来
        /// </summary>
        private void showWin()
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否退出程序", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                this.Dispose();
            }
        }

        private void 打开主界面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showWin();
        }

        private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否退出程序", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                this.Dispose();
            }
        }

        private void MainFrame_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否退出程序", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;

            }
        }

        private void MainFrame_Load(object sender, EventArgs e)
        {
            this.Text = ConfigurationManager.AppSettings["title"] ?? "小工具";

        }

        private void 导出excelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            exp();
        }
        private void exp()
        {
            saveFileDialog1.DefaultExt = "xls";
            DialogResult dr = saveFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                String filename = saveFileDialog1.FileName;
                HSSFWorkbook book1 = new HSSFWorkbook();
                ISheet dnlSheet = book1.CreateSheet("sheet1");
                IRow row1 = null;
                //设置合并样式第一行的样式值
                row1 = dnlSheet.CreateRow(0);
                //添加列值
                row1.CreateCell(0).SetCellValue("用户名");
                row1.CreateCell(1).SetCellValue("密码");
                row1.CreateCell(2).SetCellValue("状态");
                row1.CreateCell(3).SetCellValue("最后登录时间");



                //List<UserModel> us = userDal.query(bean);
                //foreach (UserModel u in us)
                //{
                //    IRow row = dnlSheet.CreateRow(dnlSheet.LastRowNum + 1);
                //    row.CreateCell(0).SetCellValue(u.username);
                //    row.CreateCell(1).SetCellValue(u.password);
                //    row.CreateCell(2).SetCellValue(u.status == 2 ? "账号错误" : "");
                //    if (u.lastLoginTime != null)
                //    {
                //        row.CreateCell(3).SetCellValue(DateTime.Parse(u.lastLoginTime).ToString("yyyy-mm-dd HH:MM:ss"));
                //    }
                //}

                System.IO.MemoryStream ms1 = new MemoryStream();
                book1.Write(ms1);

                FileStream fs1 = new FileStream(filename, FileMode.OpenOrCreate);
                BinaryWriter w1 = new BinaryWriter(fs1);
                w1.Write(ms1.ToArray());
                fs1.Close();
                ms1.Close();
                MessageBox.Show("导出成功");

            }
        }

        private void 系统配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrameFactory.ShowSettingFrame();
        }
    }
}
